package ModelImplementation

import (
	"PaySalary/Model"
	"time"
)

type UnionAffiliation struct {
	itsMemberID int
	itsDues float64
	itsServiceCharges map[time.Time]ServiceCharge
}

func (ua* UnionAffiliation)UnionAffiliation(memberId int,dues float64) {
	ua.itsMemberID = memberId
	ua.itsDues = dues
}

func (ua* UnionAffiliation) GetServiceCharge(date time.Time)ServiceCharge {
	return ua.itsServiceCharges[date]
}

func (ua* UnionAffiliation) AddServiceCharge(date time.Time,amount float64) {
	sc := serviceCharge(date,amount)
	ua.itsServiceCharges[date] = sc
}

func (ua* UnionAffiliation) GetDues() float64 {
	return ua.itsDues
}

func (ua* UnionAffiliation) GetMemberID() int {
	return ua.itsMemberID
}

func (ua *UnionAffiliation) CalculateDeductions(pc Model.Paycheck) float64 {
	totalServiceCharge, totalDues := 0.0, 0.0

	for date, sc := range ua.itsServiceCharges {
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()

		if date.After(dt1) && date.Before(dt2) {
			totalServiceCharge += sc.GetAmount()
		}
	}

	fridays := ua.numberOfFridayInPayPeriod(pc.GetPayPeriodStartDate(),
		pc.GetPayPeriodEndDate())
	totalDues = ua.itsDues * float64(fridays)
	return totalDues + totalServiceCharge
}

func (ua *UnionAffiliation) numberOfFridayInPayPeriod(payPeriodStart, payPeriodEnd time.Time) int {
	friday := 0

	for date := payPeriodStart; date.Before(payPeriodEnd); date.AddDate(0, 0, 1) {
		if date.Weekday() == time.Friday {
			friday++
		}
	}
	return friday
}

